const db = wx.cloud.database()
const app = getApp()
Page({
  data: {
    tabPanelstyle: 'justify-content:center;align-items:center;',
    auditList: [],
    companyList: [],
    enable: false,
    loadingProps: {
      size: '50rpx',
    },
  },
  onLoad: function () {
    this.getAuditData(); // 在页面加载时调用获取设备信息的函数
  },
  onTabsChange(event) {
    const { value } = event.detail;
    if (value === "1") {
      this.getCompanyData();
    }
    else if (value === "0") {
      this.getAuditData();
    }
  },
  getAuditData: function () {
    db.collection("audit")
    .orderBy('type', 'asc')
    // .where({
    //   type:false
    // })
    .get().then(res => {
      console.log(res.data);

      this.setData({
        auditList: res.data // 将获取的数据设置到页面的数据属性中
      });
    }).catch(err => {
      console.error(err);
    });
  },
  getCompanyData: function () {
    db.collection("company")
    .orderBy('type', 'asc')
    .get().then(res => {
      console.log(res.data);
      this.setData({
        companyList: res.data 
      });
    }).catch(err => {
      console.error(err);
    });
  },
  onReady() {
    this.setData({ enable: true });
    setTimeout(() => {
      this.setData({ enable: false });
    }, 1000);
  },
  onRefresh() {
    this.setData({ enable: true });
    this.getAuditData();
    this.getCompanyData();
    setTimeout(() => {
      this.setData({ enable: false });
    }, 1500);
  },
  onScroll(e) {
    const { scrollTop } = e.detail;
    this.setData({ scrollTop });
  },
  //实名认证
  onAgree(event) {
    console.log('1');
    const { index } = event.currentTarget.dataset; 
    const auditItem = this.data.auditList[index]; 
    const { _id } = auditItem; 

    db.collection("audit").doc(_id).update({
      data: {
        type: 1
      }
    }).then(res => {
      console.log("Audit item updated successfully:", res);
      
      this.getAuditData();
    }).catch(err => {
      console.error("Failed to update audit item:", err);
    });
    wx.cloud.database().collection("user").where({
      _openid: app.globalData.openid // Assuming you store openid in globalData
    }).update({
      data: {
        authen: 2,
        identity:1
      }
    }).then(() => {
      console.log('authen更新成功');
    }).catch(err => {
      console.error('authen更新失败', err);
    });
  },
  onUnAgree(event) {
    console.log('11');
    const { index } = event.currentTarget.dataset; 
    const auditItem = this.data.auditList[index]; 
    const { _id } = auditItem; 

    db.collection("audit").doc(_id).update({
      data: {
        type: 2
      }
    }).then(res => {
      console.log("Audit item updated successfully:", res);
      
      this.getAuditData();
    }).catch(err => {
      console.error("Failed to update audit item:", err);
    });
    wx.cloud.database().collection("user").where({
      _openid: app.globalData.openid // Assuming you store openid in globalData
    }).update({
      data: {
        authen: 3
      }
    }).then(() => {
      console.log('authen更新成功');
    }).catch(err => {
      console.error('authen更新失败', err);
    });
},
//公司实名认证
  onAgree1(event) {
    console.log('2');
    const { index } = event.currentTarget.dataset; 
    const companyItem = this.data.companyList[index]; 
    const { _id } = companyItem; 
  
    db.collection("company").doc(_id).update({
      data: {
        type: 1 
      }
    }).then(res => {
      console.log("Company item updated successfully:", res);
      
      this.getCompanyData();
    }).catch(err => {
      console.error("Failed to update company item:", err);
    });
    wx.cloud.database().collection("user").where({
      _openid: app.globalData.openid // Assuming you store openid in globalData
    }).update({
      data: {
        cauthen: 2,
        identity:2
      }
    }).then(() => {
      console.log('authen更新成功');
    }).catch(err => {
      console.error('authen更新失败', err);
    });
  },
  onUnAgree1(event) {
    console.log('22');
    const { index } = event.currentTarget.dataset; 
    const companyItem = this.data.companyList[index]; 
    const { _id } = companyItem; 
  
    db.collection("company").doc(_id).update({
      data: {
        type: 2
      }
    }).then(res => {
      console.log("Company item updated successfully:", res);
      
      this.getCompanyData();
    }).catch(err => {
      console.error("Failed to update company item:", err);
    });
    wx.cloud.database().collection("user").where({
      _openid: app.globalData.openid // Assuming you store openid in globalData
    }).update({
      data: {
        cauthen: 3
      }
    }).then(() => {
      console.log('authen更新成功');
    }).catch(err => {
      console.error('authen更新失败', err);
    });
}

  
})
